#include<stdio.h>
#include<stdlib.h>
#include<string.h> 

typedef struct
{
	char name[50];
	char phone[20];
	char address[100]; 
}Contact;

typedef struct Node
{
	Contact	contact;
	struct Node *next; 
}Node; 
Node *head =NULL; 
void initlist(); 
void insert(Contact data);
int searchlist(char name[]); 
void dellist(int pos);

void menu(); 
void enter();
void del();
void list();
void search();
void save();
void load();

int main()
{
	int choice;
	initlist(); 
	load();
	
	while(1)
	{
		menu();
		printf("请输入选项：");
		scanf("%d",&choice);
		while(getchar() != '\n');
		switch(choice)
		{
			case 1: enter();break;
			case 2: del();break;
			case 3: list();break;
			case 4: search();break;
			case 5: save();break;
			case 6: load();break;
			case 7: printf("退出通讯录系统");exit(0); 
			default :printf("选项无效，请重新输入\n") ; 
		} 
	}
	
	return 0; 
} 

void initlist()
{
	head =(Node*)malloc(sizeof(Node)); 
	head->next =NULL; 
}

void insert(Contact data)
{
	Node *New=(Node*)malloc(sizeof(Node));
	New->contact =data;
	New->next =head->next ;
	head->next =New;
}

int searchlist(char name[])
{
	Node *current=head->next ;
	int pos=1;
	while(current!=NULL)
	{
		if(strcmp(current->contact.name ,name ) ==0)
		{
			return pos;
		}
		current=current->next ; 
		pos++;
	}
	
	return -1;
}

void dellist(int pos)
{
	int i=1;
    Node *prev=head;
	Node *current=prev->next ;
	
	while(i!=pos)
	{
		prev=current;
		current=current->next ; 
		i++;
	} 
	prev->next =current->next ;
	free(current); 
}

void menu()
{
	printf("\n=== 通讯录管理系统 ===\n");
    printf("1. 录入联系人\n");
    printf("2. 删除联系人\n");
    printf("3. 显示所有联系人\n");
    printf("4. 查找联系人\n");
    printf("5. 保存到文件\n");
    printf("6. 从文件加载\n");
    printf("7. 退出系统\n");
}

void enter()
{
	Contact New;
	printf("===录入联系人===\n");
	printf("请输入姓名：\n");
	fgets(New.name ,50,stdin); 
	New.name[strcspn(New.name,"\n")]=0;  
	
	printf("请输入电话：\n");
	fgets(New.phone ,20,stdin);
	New.phone[strcspn(New.phone ,"\n")]=0;  
	
	printf("请输入地址：\n");
	fgets(New.address ,100,stdin);
	New.address[strcspn(New.address ,"\n")]=0;  	
	
	insert(New); 
}

void del()
{
	printf("\n===删除联系人===\n"); 
	printf("请输入要删除联系人姓名\n");
	char name[50];
	fgets(name,50,stdin);
	name[strcspn(name,"\n")] =0;
	int pos=searchlist(name) ;
	if(pos!=-1)
	{
		dellist(pos); 
		printf("成功删除\n"); 
	}
	else
	printf("不存在该联系人\n");
}

void list()
{
	printf("\n===所有联系人===\n");
	Node *current=head->next ;
	if(current==NULL) 
	{
		printf("通讯录为空\n");
		return ; 
	}
	while(current!=NULL)
	{
		printf("姓名:%s\n",current->contact.name );
		printf("电话:%s\n",current->contact.phone);
		printf("地址:%s\n",current->contact.address) ;	
		printf("\n");
		current=current->next ;			
	}
}

void search()
{
	printf("\n===查找联系人===\n");
	printf("请输入姓名：");
	char name[50];
	fgets(name,50,stdin);
	name[strcspn(name,"\n")]=0;
	
	int pos=searchlist(name);
	if(pos==-1)
		printf("未找到该联系人\n");
	else
	{
		Node *current=head->next ;
		int i=1;
		while(i!=pos)
		{
			current=current->next ;
		}
		printf("找到该联系人\n");
		printf("姓名:%s\n",current->contact.name  ); 
		printf("电话:%s\n",current->contact.phone   );
		printf("地址:%s\n",current->contact.address  );
	} 
}

void save()
{
	FILE *fp=fopen("contact.dat","w");
	if(fp==NULL)
	{
		printf("保存失败:文件不存在\n");
		return; 
	} 
	
	Node *current=head->next ;
	while(current!=NULL)
	{
		fprintf(fp,"%s|%s|%s\n",current->contact.name,current->contact.phone,current->contact.address  );
		current=current->next ;	 
	}
	fclose(fp);
	printf("已保存\n"); 
}

void load()
{	
	FILE *fp=fopen("contact.dat","r");
	if(fp==NULL)
	{
		printf("加载失败：文件不存在\n");
		return;
	} 
	
	Node *current=head->next ;
	while(current!=NULL)
	{
		Node *tem=current;
		current=current->next ;
		free(tem);
	}
	head->next =NULL; 
	
	char line[200];
	while(fgets(line,sizeof(line),fp)!=NULL)
	{
		line[strcspn(line,"\n")]=0;
		Contact data;
		int i=0,count=0;
		char copy[200];
		while(line[i]!='|')
		{
			data.name[count]=line[i]; 
			i++;
			count++;
		} 
		i++;
		data.name[count]=0;
		
		count=0;
		while(line[i]!='|')
		{
			data.phone[count]=line[i]; 
			i++;
			count++;
		} 
		i++;
		data.phone[count]=0;

		count=0;
		while(line[i]!=0)
		{
			data.address[count]=line[i]; 
			i++;
			count++;
		} 		
		data.address[count]=0;
		
		insert(data);	
	} 
	
	fclose(fp); 
	printf("加载成功\n"); 
}

